package com.sukaiyi.generator.ui.renderer;

import com.sukaiyi.generator.config.model.StructTemplate;
import com.sukaiyi.generator.config.model.TplType;
import com.sukaiyi.generator.utils.IdeaUIUtils;
import com.sukaiyi.generator.utils.StringUtils;
import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTreeCellRenderer;
import icons.PluginIcons;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class TemplateTreeNodeRenderer extends ColoredTreeCellRenderer {
    @Override
    public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if(value instanceof DefaultMutableTreeNode){
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            Object userObject = node.getUserObject();
            if(userObject == null || userObject.getClass() != StructTemplate.class){
                return;
            }
            StructTemplate tpl = (StructTemplate) userObject;
            append(tpl.getName());
            if(node.getParent() == null){
                setIcon(PluginIcons.ENTITY);
                return;
            }
            if (tpl.getType() == TplType.TEMPLATE) {
                String suffix = StringUtils.getFileSuffix(tpl.getName());
                setIcon(IdeaUIUtils.getIconByFileSuffix(suffix));
            } else {
                setIcon(AllIcons.Nodes.Package);
            }
        }
    }
}
